package com.easy.life.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easy.life.common.R;
import com.easy.life.dao.OrderDao;
import com.easy.life.dao.RoomDao;
import com.easy.life.dao.UserDao;
import com.easy.life.entity.OrderEntity;
import com.easy.life.entity.RoomEntity;
import com.easy.life.entity.UserEntity;
import com.easy.life.redis.impl.StringRedisServiceImpl;
import com.easy.life.service.OrderService;
import com.easy.life.utils.NumberRandomUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private UserDao userDao;
    @Resource
    private RoomDao roomDao;
    @Resource
    private StringRedisServiceImpl stringRedisService;

    @Override
    @Transactional
    public R rentRoom(Integer way, Long userId, Integer day, Long roomId) {
        if (way==1){
            RoomEntity roomEntity = roomDao.selectById(roomId);
            roomEntity.setStatus(2);
            BigDecimal price = roomEntity.getPrice();
            BigDecimal allPrice=price.multiply(BigDecimal.valueOf(day));
            UserEntity user = userDao.selectById(userId);
            if (user.getBalance().compareTo(allPrice)==-1){
                return R.error(500,"余额不足，请换种方式支付");
            }else {
                user.setBalance(user.getBalance().subtract(allPrice));
                Long businessId = roomEntity.getUserId();
                UserEntity business = userDao.selectById(businessId);
                business.setBalance(business.getBalance().add(allPrice));
                userDao.updateById(user);
                userDao.updateById(business);
                roomDao.updateById(roomEntity);
                OrderEntity order=new OrderEntity();
                order.setDays(day);
                order.setMoney(allPrice);
                order.setRoomId(roomId);
                order.setUserId(userId);
                order.setWay(way);
                orderDao.insert(order);
                String accessCode= NumberRandomUtil.randomNumBuilder();
                stringRedisService.set(order.getId().toString(),accessCode);
                stringRedisService.expire(order.getId().toString(),order.getDays()*86164);
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("msg","支付成功！");
                hashMap.put("access",accessCode);
                return R.ok().put("data",hashMap);
            }

        }
        return R.ok().put("data","现在只支持余额付款！");
    }

    @Override
    public R getOrders(Long id) {
        List<OrderEntity> orderEntityList = orderDao.selectList(new QueryWrapper<OrderEntity>().eq("userId", id));
        return R.ok().put("data",orderEntityList);
    }

    @Override
    public R getAccessCode(Long orderId) {
        Boolean aBoolean = stringRedisService.hasKey(orderId.toString());
        if (aBoolean) {
            String accessCode= (String) stringRedisService.get(orderId.toString());
            return R.ok().put("accessCode",accessCode);
        }else {
            return R.error(404,"订单已完成");
        }

    }

}
